package my.action.article;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import javax.servlet.http.*;

import my.controller.*;
import my.db.DAO.ArticleDAO;
import my.db.DTO.ArticleDTO;
import my.db.DTO.ArticleTypeDTO;
public class ArticleAddAction implements  Action{
	public Date getMachineTime(){
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		Calendar cal = Calendar.getInstance();
		return cal.getTime();
	}
	 public ActionForward execute(HttpServletRequest request,HttpServletResponse response) throws Exception{			
		    ArticleDAO article=new ArticleDAO();
		 	ActionForward forward=new ActionForward();
		 	String title=request.getParameter("articleTitle");
		 	String content=request.getParameter("articleContent");
		 	int typeID=Integer.parseInt(request.getParameter("articleTypeID"));
		   	boolean result=true;
		   	ArticleDTO ar=new ArticleDTO();
		   	ar.setArticleNo(104);
		   	ar.setArticleTitle(title);
		   	ar.setArticleContent(content);
		    ar.getArticleType().setTypeID(typeID);
            ar.getAuthor().setAuthorID(1);     
		   	ar.setArticleDate(getMachineTime());
		   	ar.setArticleStatus(1);
		   	result=article.addArticle(ar);
		   	System.out.println(ar.toString());
		   	forward.setRedirect(false);
	   		try{ 	   			
	   			if(result==true){   
	   				forward.setPath("./addaccount.jsp?error=successfully added");
	   			}else{	   				
	   				forward.setPath("./addaccount.jsp?error=unsuccessfully added");
	   			}
	  		}catch(Exception ex){
	  			System.out.println(ex);
	   		}
	  		return forward;
		}  	
}